from PyQt5.QtWidgets import QApplication,QWidget,QMessageBox
import sys


class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.setGeometry(1000,300,300,300)
        self.setWindowTitle('消息框')
        self.show()

    # 事件函数会在相应的事件发生时被自动调用
    def closeEvent(self, event):
        # 创建消息框
        reply = QMessageBox.question(self,'消息框','你是否确定要退出当前程序？',QMessageBox.Yes | QMessageBox.No)

        # 判断用户的选择
        if reply == QMessageBox.Yes:
            # 确定退出
            event.accept()
        else:
            # 取消退出
            event.ignore()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Window()
    app.exec()